# --*-- coding=utf-8 --*--
"""
测试计划主程序入口
"""
import os
import unittest
from datetime import datetime
from BeautifulReport import BeautifulReport
from auto_test.config import CBSConfig
from auto_test.test.parse.parse_data import getTestPlan
from auto_test.test.entity.case import TestCase


def create_case_script():
    """
    生成测试脚本
    """
    fp = CBSConfig.TEST_ROOT_DIR + '/test/case/test_plan.py'
    if os.path.exists(fp):
        os.remove(fp)
    test_plan = getTestPlan()
    with open(fp, 'a+', encoding='utf-8') as f:
        f.write('# --*--coding=utf-8 --*--\n')
        f.write('import unittest\n')
        f.write('from auto_test.test.parse.parse_data import exec_case_by_id\n\n\n')
        f.write('class TestPlan(unittest.TestCase):\n\n')
        if len(test_plan.execCaseList) == 0:
            f.write('    pass\n')
            return
        for case in test_plan.execCaseList:
            if not isinstance(case, TestCase):
                continue
            f.write('    def test_%s(self):\n' % case.case_id)
            f.write('        """%s"""\n' % case.case_name)
            f.write('        self.assertEqual(True, exec_case_by_id("%s"))\n\n' % case.case_id)
        f.flush()
        f.close()


if __name__ == '__main__':
    create_case_script()
    discover = unittest.defaultTestLoader.discover(CBSConfig.TEST_ROOT_DIR + '/test/case', 'test_plan.py')
    filename = datetime.now().strftime('%Y%m%d_%H%M%S')
    br = BeautifulReport(discover)
    br.report(description='CBS自动化测试', filename=filename, report_dir=CBSConfig.TEST_REPORT_DIR)
